package com.appstore.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.appstore.dao.ICommentDao;
import com.appstore.exception.DaoException;
import com.appstore.model.Application;
import com.appstore.model.Comment;
import com.appstore.util.ConnectionManager;

/**
 * The implementation of ICommentDao.
 * 
 * @author zhbhun
 * 
 */
public class CommentDaoImpl implements ICommentDao {

	private static CommentDaoImpl commentDaoImpl;
	private ConnectionManager connectionManager;

	private CommentDaoImpl() {
		connectionManager = ConnectionManager.getConnectionManager();
	}

	/**
	 * instance CommentDaoImpl
	 * 
	 * @return
	 */
	public static CommentDaoImpl createInstatce() {
		if (commentDaoImpl == null) {
			commentDaoImpl = new CommentDaoImpl();
		}
		return commentDaoImpl;
	}

	/**
	 * get the instance of ApplicationDaoImpl
	 * 
	 * @return
	 */
	public static CommentDaoImpl getCommentDaoImpl() {
		return commentDaoImpl;
	}

	@Override
	public void save(Comment comment) throws DaoException {
		Connection connection = null;
		// get connection
		try {
			connection = connectionManager.getConnetion();
		} catch (SQLException e) {
			throw new DaoException("can not get connection", e);
		}
		// save data
		try {
			String sql = "insert into comment (containt,created_time,application_id) values(?,?,?)";
			connection.setAutoCommit(false);
			PreparedStatement pStatement = connection.prepareStatement(sql);
			pStatement.setString(1, comment.getContaint());
			pStatement.setTimestamp(2, comment.getCreatedTime());
			pStatement.setInt(3, comment.getApplicationId());
			pStatement.executeUpdate();
			pStatement.close();
			connection.commit();
		} catch (SQLException e) {
			try {
				connection.rollback();
			} catch (SQLException e1) {
				throw new DaoException("there is error while rollback", e);
			}
			throw new DaoException("there is error while executing sql", e);
		}
		// close connection
		try {
			connection.close();
		} catch (SQLException e) {
			throw new DaoException("can not close connection", e);
		}

	}

	@Override
    public List<Comment> findByApp(int appId) throws DaoException {
		List<Comment> list = new ArrayList<Comment>();
		Connection connection = null;
		// get connection
		try {
			connection = connectionManager.getConnetion();
		} catch (SQLException e) {
			throw new DaoException("can not get connection", e);
		}
		// save data
		try {
			String sql = "select * from comment where application_id = ? order by created_time desc";
			connection.setAutoCommit(false);
			PreparedStatement pStatement = connection.prepareStatement(sql);
			pStatement.setInt(1, appId);
			ResultSet resultSet = pStatement.executeQuery();
			while (resultSet.next()) {
				Comment comment = new Comment();
				comment.setId(resultSet.getInt("id"));
				comment.setContaint(resultSet.getString("containt"));
				comment.setCreatedTime(resultSet.getTimestamp("created_time"));
				comment.setApplicationId(resultSet.getInt("application_id"));
				list.add(comment);
			}
			pStatement.close();
			connection.commit();
		} catch (SQLException e) {
			try {
				connection.rollback();
			} catch (SQLException e1) {
				throw new DaoException("there is error while rollback", e);
			}
			throw new DaoException("there is error while executing sql", e);
		}
		// close connection
		try {
			connection.close();
		} catch (SQLException e) {
			throw new DaoException("can not close connection", e);
		}
		return list;
	}

}
